package com.david.core.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences

class PreferenceUtils {

    companion object{
        @SuppressLint("StaticFieldLeak")
        var context:Context? = null;
        @SuppressLint("StaticFieldLeak")
        private lateinit var instance:PreferenceUtils
        var preferences: SharedPreferences? = null

        public fun getInstance():PreferenceUtils{
            if (!this::instance.isInitialized) {
                instance = PreferenceUtils()
                preferences = context?.getSharedPreferences(
                    "prefer_data",
                    Context.MODE_PRIVATE
                )
            }
            return instance;
        }
    }

    fun setString(name: String, value:String) {
        preferences?.edit()?.putString(name, value)?.commit()
    }

    fun getString(name: String):String {
        return preferences?.getString(name, "")!!
    }

    fun getString(name: String, default: String):String? {
        return preferences?.getString(name, default)
    }

    fun setLong(name:String, value:Long){
        preferences?.edit()?.putLong(name, value)?.commit()
    }

    fun getLong(name: String):Long {
        return preferences?.getLong(name, -1)!!
    }

    fun getLong(name: String, default:Long):Long {
        return preferences?.getLong(name, default)?:-1
    }

    fun setInt(name:String, value:Int){
        preferences?.edit()?.putInt(name, value)?.commit()
    }

    fun getInt(name: String):Int {
        return preferences!!.getInt(name, -1)
    }

    fun getInt(name: String, default: Int):Int {
        return preferences!!.getInt(name, default)
    }

}
